$(document).ready(function(){
	$("#hotelSearchForm").submit( function () {
		$(".cityname").hide();
		$(".hotelnearby").hide();
		$("#cityresults").hide();
		if($("#cityname").val()=="") {
			$("#cityname").focus();
			alert("城市不能为空，请你选择城市");
			return false;
		}
		if($("#cityname").val() != "" && $("#cityid").val() =="") {
			setcityid($("#cityname").val());
		}
		if($("#cityname").val() == "" || $("#cityid").val() =="") {
			//var r = setcityid($("#cityname").val());
			//if(!r) {
				alert("对不起，城市输入有误，请重新输入，可以输入拼音↑↓选择进行选择。");
				return false;
			//}
		}
		if($("#tm1").val()=="") {
			$("#tm1").focus();
			alert("入住日期不能为空");
			return false;
		}
		if($("#tm2").val()=="") {
			$("#tm2").focus();
			alert("离店日期不能为空");
			return false;
		}
		if($("#tm1").val() != '' && $("#tm2").val() != '') {
			if(!checkdate($("#tm1").val()) || !checkdate($("#tm2").val())) {
				if(!checkdate($("#tm1").val())) {
					$("#tm1").focus();	
				} else if(!checkdate($("#tm2").val())) {
					$("#tm2").focus();	
				}
				alert("日期格式错误，请重新选择");
				return false;
			}
			var d = gethasdate($("#tm1").val(),$("#tm2").val());
			if(d >= 0) {
				$("#tm2").focus();
				alert("日期选择错误，入住日期不能大于等于离开日期！");
				return false;
			} else if(d <= -60) {
				$("#tm2").focus();
				alert("入住日期和离店日期不能晚于2个月之后！");
				return false;
			}
		}
		if($("#hotelname").val()!=""){
			if($("#hotelname").val().match(/[\'\"\<\>\,\;\^\$\(\)\!\.]+/ig)!=null) {
				$("#hotelname").focus();
				alert("酒店名称存在非法字符，请重新填写");
				return false;
			}
		}
		if($("#hotelname").val() == hotelname_tips) { $("#hotelname").val('');}
		if($("#hotelnearby").val() == hotelnearby_tips) { $("#hotelnearby").val('');}
		searchtips($("#submitbotton"));
	});	
});
function urlarray(str) {
    var newArray = new Array();
    var urlarrayStr = str.split("&");
    var valname = null;
    for (var i = 0; i < urlarrayStr.length; i++) {
        valname = urlarrayStr[i].split("=");
        newArray[valname[0]] = decodeURIComponent(valname[1])
    }
    return newArray
}
function getminusdate(startday,endday)
{
	var fArray = startday.split('-');
	var sArray = endday.split('-');
	
	var fDate = new Date(parseInt(fArray[0]),(parseInt(fArray[1],10)-1),parseInt(fArray[2],10));
	var sDate = new Date(parseInt(sArray[0]),(parseInt(sArray[1],10)-1),parseInt(sArray[2],10));
	
	var t = Math.abs(fDate.getTime()-sDate.getTime());
	var days = t/(24 * 3600 * 1000);
	return days;
}
function gethasdate(startday,endday) {
	var fArray = startday.split('-');
	var sArray = endday.split('-');
	
	var fDate = new Date(parseInt(fArray[0]),(parseInt(fArray[1],10)-1),parseInt(fArray[2],10));
	var sDate = new Date(parseInt(sArray[0]),(parseInt(sArray[1],10)-1),parseInt(sArray[2],10));
	
	var t = fDate.getTime()-sDate.getTime();
	var days = t/(24 * 3600 * 1000);
	return days;
}
Date.prototype.format = function(format)      
{      
   var o = {      
	 "M+" : this.getMonth()+1, //month      
	 "d+" : this.getDate(),    //day      
	 "h+" : this.getHours(),   //hour      
	 "m+" : this.getMinutes(), //minute      
	 "s+" : this.getSeconds(), //second      
	 "q+" : Math.floor((this.getMonth()+3)/3), 
	 //quarter      
	 "S" : this.getMilliseconds() //millisecond    
   }      
   if(/(y+)/.test(format)) format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));      
   for(var k in o)if(new RegExp("("+ k +")").test(format))      
   format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] :("00"+ o[k]).substr((""+ o[k]).length));     
   return format;      
}
function checkdate(datestr) 
{ 
    var   reg=/\d{4}-\d{1,2}-\d{1,2}/ 
    if(reg.test(datestr)){return   true} 
    else{return   false} 
    var   str=f1.text.value 
    var   c_str=str.splite( "- ") 
    if(c_str[1]> 12){return   false}
    else   if(c_str[2]> 31){return   fale}
}
function setNearValue(coord,name){
	$("#hotelnearby").val(name);
	$("#coordinate").val(coord);
	$("#autoCompleteNearby").hide();
	$("select").show();
}
function setCityValue(id,name) {
	$("#cityname").val(name);
	$("#cityid").val(id);
	$("#autoCompleteCityName").hide();
	$("select").show();
}
function setCityValue2(id,name) {
	$("#cityname").val(name);
	$("#cityid").val(id);
	$("#cityresults").hide();
	$("select").show();
}
function setCityValue3(id,name) {
	var offset = $("#cityname").offset();
	var offset2 = $("#cityresults").offset();
	if(offset.left == offset2.left) {
		$("#cityname").val(name);
		$("#cityid").val(id);
	} else {
		$("#mcityname").val(name);
		$("#mcityid").val(id);
	}
	$("#cityresults").hide();
	$("select").show();
}
function setcityid(cityname) {
	//var re=false;
	for(var i=0;i<cityNameList.length;i++) {
		var patt1 = new RegExp("^"+cityname,"i");
		if(patt1.test(cityNameList[i].Name)) {
			$("#cityid").val(cityNameList[i].Id);
			//re=true;
		}
	}
	//return re;
}
function searchtips(obj) {
	var offset = $(obj).offset();
	var h = offset.top;
	var w = offset.left + 150;
	var l = w;
	var t = h-($("#searchtips").height()/2);
	$("#searchtips").css({left:l,top:t});
	$("#searchtips").show();
}
//提交订单 hid:酒店编号 rid:房型编号 pid:房价排期编号 tm1:入住日期 tm2:退房日期
function submitorder(hid,rid,pid,tm1,tm2) {
	$.ajax({
	   url: siteurl+"index.php",
	   async: false,
	   data: "ac=hotel&at=hotelrooms&hid="+hid+"&rid="+rid+"&pid="+pid+"&tm1="+tm1+"&tm2="+tm2+"&ran="+Math.random(),
	   success: function(data){
			$("#submittips").hide();
			if(data == 0) {
				alert("对不起，该房型已经满房，请选择其它房型预订。");	
			} else {
				$("#hid").val(hid);
				$("#rid").val(rid);
				$("#pid").val(pid);
				$("#ordertm1").val(tm1);
				$("#ordertm2").val(tm2);
				$("#ordersubmit").submit();
			}
	   }
	});	
}

function ordertips(hid,rid,pid) {
	$(function() {
		$("<div id='submittips' style='border:1px solid #949494; background:url("+siteurl+"/templates/greenhotel/images/loading36.gif) 25px 8px no-repeat; width:300px; height:60px; line-height:60px; display:none; position:absolute;color:#0069ca; background-color:#fff; z-index:999;'>正在读取实时房态请稍后...</div>").insertAfter("#tips_rooms");
	});
	var offset = $("#"+hid+rid+"_"+pid).offset();
	var w = $("#"+hid+rid+"_"+pid).width()-$("#submittips").width();
	$("#submittips").css({ left:offset.left+w, top:offset.top+25});
	$("#submittips").show();
}